import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决重复导航问题
// 获取原型对象push函数
const originalPush = VueRouter.prototype.push
// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace
// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "Index",
    redirect: '/login',
    component: () => import('@/views/Index.vue'),
    children: [
      {
        path: 'login',
        name: 'Login',
        component: () => import('@/views/index/Login.vue'),
        meta: {title: '登录 | 库赞在线考试系统教师端'}
      },
      {
        path: '404',
        name: 'Page404',
        component: () => import('@/views/error-page/404.vue'),
        meta: {title: '页面未找到'}
      },
      {
        path: '401',
        name: 'Page401',
        component: () => import('@/views/error-page/401.vue'),
        meta: {title: '权限错误'}
      },
    ]
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    redirect: '/dashboard/home',
    meta: {title: '控制台'},
    component: () => import('@/views/Dashboard.vue'),
    children: [
      {
        path: 'home',
        name: 'Home',
        meta: {title: '首页'},
        component: () => import('@/views/dashboard/Home.vue'),
      },
      {
        path: 'person',
        name: 'Person',
        redirect: '/dashboard/person/detail',
        component: () => import('@/views/dashboard/person/Person.vue'),
        meta: {title: '个人主页'},
        children: [
          {
            path: 'detail',
            name: 'DetailShow',
            meta: {title: '个人信息'},
            component: () => import('@/views/dashboard/person/DetailShow.vue'),
          },
          {
            path: 'username',
            name: 'UsernameModify',
            meta: {title: '修改用户名'},
            component: () => import('@/views/dashboard/person/UsernameModify.vue'),
          },
          {
            path: 'password',
            name: 'PasswordModify',
            meta: {title: '修改密码'},
            component: () => import('@/views/dashboard/person/PasswordModify.vue'),
          },
        ],
      },
      {
        path: 'subject',
        name: 'SubjectPage',
        meta: {title: '学科列表'},
        component: () => import('@/views/dashboard/subject/SubjectPage.vue'),
      },
      {
        path: 'subject/edit',
        name: 'SubjectEdit',
        meta: {title: '学科编辑'},
        component: () => import('@/views/dashboard/subject/SubjectEdit.vue'),
      },
      {
        path: 'subject/add',
        name: 'SubjectAdd',
        meta: {title: '添加学科'},
        component: () => import('@/views/dashboard/subject/SubjectAdd.vue'),
      },
      {
        path: 'examPaper',
        name: 'ExamPaperPage',
        meta: {title: '试卷列表'},
        component: () => import('@/views/dashboard/exampaper/ExamPaperPage.vue'),
      },
      {
        path: 'examPaper/add',
        name: 'ExamPaperAdd',
        meta: {title: '添加试卷'},
        component: () => import('@/views/dashboard/exampaper/ExamPaperAdd.vue'),
      },
      {
        path: 'question',
        name: 'QuestionPage',
        meta: {title: '题目列表'},
        component: () => import('@/views/dashboard/question/QuestionPage.vue'),
      },
      {
        path: 'question/single',
        name: 'SingleQuestion',
        meta: {title: '单选题创建'},
        component: () => import('@/views/dashboard/question/SingleQuestion.vue'),
      },
      {
        path: 'question/multiple',
        name: 'MultipleQuestion',
        meta: {title: '多选题创建'},
        component: () => import('@/views/dashboard/question/MultipleQuestion.vue'),
      },
      {
        path: 'question/judgement',
        name: 'JudgementQuestion',
        meta: {title: '判定题创建'},
        component: () => import('@/views/dashboard/question/JudgementQuestion.vue'),
      },
      {
        path: 'question/blank',
        name: 'BlankQuestion',
        meta: {title: '填空题创建'},
        component: () => import('@/views/dashboard/question/BlankQuestion.vue'),
      },
      {
        path: 'question/short',
        name: 'ShortQuestion',
        meta: {title: '简答题创建'},
        component: () => import('@/views/dashboard/question/ShortQuestion.vue'),
      },
      {
        path: 'answerPaper/doing',
        name: 'AnswerPaperDoing',
        meta: {title: '批改列表'},
        component: () => import('@/views/dashboard/answerpaper/AnswerPaperDoing.vue'),
      },
      {
        path: 'answerPaper/done',
        name: 'AnswerPaperDone',
        meta: {title: '批改完成'},
        component: () => import('@/views/dashboard/answerpaper/AnswerPaperDone.vue'),
      },
      {
        path: 'answerPaper/edit',
        name: 'AnswerPaperEdit',
        meta: {title: '答卷批阅'},
        component: () => import('@/views/dashboard/answerpaper/AnswerPaperEdit.vue'),
      },
      {
        path: 'answerPaper/read',
        name: 'AnswerPaperRead',
        meta: {title: '答卷查看'},
        component: () => import('@/views/dashboard/answerpaper/AnswerPaperRead.vue'),
      },
    ]
  },
  {
    path: '*',
    redirect: '/404'
  },
]

const router = new VueRouter({
  routes
})

// 全局守卫
// 注意，以下两个守卫除了在各自的执行时机执行外，网页初始化时也会执行一次
// 前置守卫
router.beforeEach((to, from, next) => {
    // 防止重复导航
    // if (to.path === from.path) {
    //   return false;
    // }
    if (to.name === 'Login' || to.name === 'Register') {
      return next();
    }
    const currentUser = localStorage.getItem('current-user');
    if (currentUser === null) {
      Vue.prototype.$alert('请登录', '提示', {
        confirmButtonText: '确定',
        callback() {
          next('/login');
        }
      });
    } else {
      next();
    }
  }
)

// 后置守卫
router.afterEach((to, from) => {
  // 设置每个路由的标题显示
  document.title = to.meta.title;
})
export default router
